//
//  AllViewController.m
//  LOLVideoPlayer
//
//  Created by SXW on 16/3/25.
//  Copyright © 2016年 盛宣伟. All rights reserved.
//

#import "AllViewController.h"
#import "AllTableViewCell.h"
#import "AllDetailController.h"
#import "OneModel.h"
#import "OneTableViewCell.h"
#import "SXHTTPEngine.h"
@interface AllViewController ()

@property (nonatomic, copy) NSString *urls;
@property(nonatomic,strong)NSMutableArray *bannersArry;
@end

@implementation AllViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    /**
     *  http://box.dwstatic.com/apiNewsList.php?action=l&newsTag=headlineNews&p=
     */
    [self createScorllView];
    _urls = @"http://www.demaxiya.com/app/index.php?m=news&showcount=%ld&typeid=%ld&__ii=865168026985233&__aa=31ebff81777ab5e3";
    [self initWork];
    
}

- (void)createScorllView{
 
//    [self initData:^(id data) {
//      
//        NSDictionary *dataDic = data;
//        //取出顶部banner视图的数据
//        NSArray *bannerArry = [dataDic objectForKey:@"headerline"];
//        //通过遍历
//        for (int i =0; i<bannerArry.count; i++) {
//            OneModel *banner = [OneModel mj_objectWithKeyValues:bannerArry[i]];
//            [self.bannersArry addObject:banner];
//        }
//    }];
    [[SXHTTPEngine shareManager] GET:@"http://box.dwstatic.com/apiNewsList.php?action=l&newsTag=headlineNews&p=1" params:nil success:^(id responseObject) {
        
        NSArray *arr = responseObject[@"headerline"];
        self.bannersArry = [OneModel arrayOfModelsFromDictionaries:arr];
         NSLog(@"大个%@",self.bannersArry);
    } failure:^(NSError *error) {
        
        NSLog(@"数据请求失败");
    }];
}

-(NSMutableArray *)bannersArry{

    if (!_bannersArry) {
        _bannersArry = [[NSMutableArray alloc] init];
    }
    return _bannersArry;
}

- (void)initWork{

    self.urlStr = [NSString stringWithFormat:_urls,self.pageNum];
    [self initData:^(id data) {
        
        NSDictionary *dataDic = data;
        NSArray *dataArray = dataDic[@"list"];
//        NSLog(@"啦啦%@",dataArray);
        for (int i = 0; i < dataArray.count; i++) {
            InfoModel *model = [InfoModel mj_objectWithKeyValues:dataArray[i]];
            [self.jsonArray addObject:model];
//            ccLog(@"啦啦%@",model);
        }
        self.tableViewframe = CGRectMake(0, -20, APPScreenWidth, APPScreenHeight + 20 - 74 - 49);
        [self.view addSubview:self.tableView];
        [self.tableView reloadData];
//        ccLog(@"dataArray:%@",dataArray);
    }];
}
//加载更多
-(void)loadData{
    
    self.pageNum += 12;
    [self initWork];
    [self.tableView.mj_footer endRefreshing];
}

//下拉刷新
-(void)refreshData{
    
    self.pageNum = 0;
    [self.jsonArray removeAllObjects];
    [self initWork];
    [self.tableView.mj_header endRefreshing];
}

#pragma mark - cell设置

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

    return 2;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    if (section == 1) {
        return self.jsonArray.count;
    }else{
    
        return 1;
    }
    
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    if (indexPath.section == 0) {
        static NSString *cellId = @"cell";
        OneTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
        if (cell == nil) {
            cell = [[OneTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.dataArray = self.bannersArry;
        return cell;
    }
    else{
    
        static NSString *cellID = @"cell_all";
        AllTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
        if (cell == nil) {
            cell = [[AllTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        if (self.jsonArray.count > 0) {
            InfoModel *model = [self.jsonArray objectAtIndex:indexPath.row];
            cell.model = model;
        }
        return cell;
    }
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    InfoModel *model = self.jsonArray[indexPath.row];
    AllDetailController *dt = [AllDetailController new];
    dt.ids = model.ids;
    [self.navigationController pushViewController:dt animated:YES];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    if (indexPath.section == 0) {
        return 200;
    }else{
    
        return [AllTableViewCell getCellHeight] - 10;
    }
}















@end
